<?php


namespace App\Http\Controllers\Api;


use App\Models\ImFtfGroup;
use Illuminate\Http\Request;

class FTFGroupController
{
    /**
     * 创建临时群
     * @param Request $req
     * @return \Illuminate\Http\JsonResponse
     */
    public function createGroup(Request $req)
    {
        $user_id = $req->user()['id'];
        $longitude = $req->post('longitude');
        $latitude = $req->post('latitude');
        $password = $req->post('password');
        if (empty($longitude) || empty($latitude) || empty($password)) {
            return errBack();
        }
        if (strlen($password) < 4) {
            return errBack('密码不能小于4位');
        }
        $ftf = new ImFtfGroup();
        $res = $ftf->createGroup([
            'user_id' => $user_id,
            'longitude' => $longitude,
            'latitude' => $latitude,
            'password' => $password,
        ]);
        if ($res['code'] !== 0) {
            return errBack($res['msg']);
        }
        return okBack($res['data']);
    }

    /**
     * 设置群状态
     * @param Request $req
     * @return \Illuminate\Http\JsonResponse
     */
    public function setStatus(Request $req)
    {
        $id = $req->post('id');
        $user_id = $req->user()['id'];
        $status = $req->post('status');
        $ftf = new ImFtfGroup();
        $res = $ftf->setStatus([
            'id' => $id,
            'user_id' => $user_id,
            'status' => $status,
        ]);
        if ($res['code'] !== 0) {
            return errBack($res['msg']);
        }
        return okBack();
    }

    /**
     * 获取附近群组
     * @param Request $req
     * @return \Illuminate\Http\JsonResponse
     */
    public function nearbyGroup(Request $req)
    {
        $longitude = $req->get('longitude');
        $latitude = $req->get('latitude');
        $password = $req->get('password');
        if (empty($longitude) || empty($latitude) || empty($password)) {
            return errBack();
        }
        $ftf = new ImFtfGroup();
        $res = $ftf->nearbyGroup([
            'longitude' => $longitude,
            'latitude' => $latitude,
            'password' => $password,
        ]);
        if ($res['code'] !== 0) {
            return errBack($res['msg']);
        }
        return okBack($res['data']);
    }

    /**
     * 退出临时群
     * @param Request $req
     * @return \Illuminate\Http\JsonResponse
     */
    public function memberExit(Request $req)
    {
        $ftf_id = $req->post('ftf_id');
        $user_id = $req->user()['id'];
        if ($ftf_id <= 0 || $user_id <= 0) {
            return errBack();
        }
        $ftf = new ImFtfGroup();
        $res = $ftf->memberExit([
            'ftf_id' => $ftf_id,
            'user_id' => $user_id
        ]);
        if ($res['code'] !== 0) {
            return errBack($res['msg']);
        }
        return okBack();
    }

    /**
     * 从临时群进入正式群
     * @param Request $req
     * @return \Illuminate\Http\JsonResponse
     */
    public function intoGroup(Request $req)
    {
        $ftf_id = $req->post('ftf_id');
        $user_id = $req->user()['id'];
        if ($ftf_id <= 0 || $user_id <= 0) {
            return errBack();
        }
        $ftfGroup = new ImFtfGroup();
        $res = $ftfGroup->intoGroup([
            'ftf_id' => $ftf_id,
            'user_id' => $user_id,
        ]);
        if ($res['code'] !== 0) {
            return errBack($res['msg']);
        }
        return okBack('success', ['group_id' => $res['data']]);
    }

    /**
     * 将过期的临时群关闭
     * @param Request $req
     * @return \Illuminate\Http\JsonResponse
     */
    public function checkTimeout(Request $req)
    {
        ImFtfGroup::where('timeout', '<', time())
            ->whereBetween('status', [1, 2])->update([
                'status' => 3
            ]);
        return okBack();
    }
}
